package com.louis.springboot.demo.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.louis.springboot.demo.model.User;
import com.louis.springboot.demo.service.UserService;

/**
 * 系统用户服务实现，用于从用户数据库里获得用户密码和权限
 */
@Service
public class SysUserServiceImpl implements UserService {


	ArrayList<User> list = new ArrayList<>();
	@Override
	public User findByUsername(String username) {

		if (list.size() == 0)
		{
			User user = new User();
			user.setId(1L);
			user.setUsername("admin");
			user.setPassword(new BCryptPasswordEncoder().encode("admin"));
			list.add(user);

			user = new User();
			user.setId(1L);
			user.setUsername("pzf");
			user.setPassword(new BCryptPasswordEncoder().encode("pzf123456"));
			list.add(user);
		}
		User user = new User();
		user.setId(1L);
		user.setUsername(username);
		String password = "";
		if(username.equals("admin")) {
			return list.get(0);//password = new BCryptPasswordEncoder().encode("admin");
		}
		else if (username.equals("pzf"))
		{
			return list.get(1);
		}
		else {
			password = new BCryptPasswordEncoder().encode(username);
		}
		user.setPassword(password);
		return user;
	}

	@Override
	public Set<String> findPermissions(String username) {
		Set<String> permissions = new HashSet<>();
		if (username.equals("admin")) {

			permissions.add("sys:user:view");
			permissions.add("sys:user:add");
			permissions.add("sys:user:edit");
			permissions.add("sys:user:delete");
		}
		else if (username.equals("pzf"))
		{
			permissions.add("sys:user:edit");
		}
		else {
			permissions.add("sys:user:view");
		}
		return permissions;
	}

}
